const assert = require("assert");
const Cache = require("../src/cache.js");
const Redis = require("../src/redis.js");
const Ret = require("../src/ret.js");

describe("cache.js", () => {
  before(() => {
    console.log("init");
    Redis.init("127.0.0.1", "default", "KS5UggH4LNyyLBdr");
  });

  describe("get()", () => {
    it("return string", async () => {
      let testFunction = (a, b) => {
        console.log(a + b);
        return a + b + "";
      };
      let result = await Cache.get(
        "test",
        20,
        testFunction,
        testFunction,
        1,
        2
      );
      console.log("result", result);
      assert(result === "3");
    });
    it("return object", async () => {
      let testFunction = (a, b) => {
        console.log(a + b);
        return { r: a + b };
      };
      try {
        let result = await Cache.get(
          "test2",
          20,
          testFunction,
          testFunction,
          1,
          2
        );
      } catch (e) {
        assert(e.message === "只支持返回值是文本的方法");
      }
    });
  });

  describe("auto()", () => {
    it("return string", async () => {
      async function test(a, b) {
        return await Cache.auto("test", 20, arguments, () => {
          console.log(a + b);
          return a + b + "";
        });
      }
      const result = await test(1, 2);
      console.log("result", result);
      assert(result === "3");
    });
    it("return int", async () => {
      async function test(a, b) {
        let r = await Cache.auto("test2", 20, arguments, () => {
          console.log(a + b);
          return a + b;
        });
        return r;
      }
      const result = await test(1, 2);
      console.log("result", result);
      assert(result == 3);
    });
    it("return object", async () => {
      async function test(a, b) {
        return await Cache.auto("test3", 20, arguments, () => {
          console.log(a, b);
          return { a, b };
        });
      }
      const result = await test(1, 2);
      console.log("result", JSON.stringify(result));
      const compare = { a: 1, b: 2 };
      assert(result.a === compare.a);
    });
    it("return array", async () => {
      async function test(a, b) {
        return await Cache.auto("test4", 20, arguments, () => {
          console.log(a, b);
          return [{ a, b }];
        });
      }
      const result = await test(1, 2);
      console.log("result", JSON.stringify(result));
      assert(result.length === 1);
    });
    it("no cache fail", async () => {
      async function test(a, b) {
        return await Cache.auto("test5", 20, arguments, () => {
          return new Ret(false);
        });
      }
      const result = await test(1, 2);
      console.log("result", JSON.stringify(result));
      assert(result.success === false);
    });

    it("auto base64", async () => {
      let a = 1,
        b = 2;
      let result = await Cache.auto(
        "test",
        20,
        [],
        async () => {
          console.log(a + b);
          return a + b + "";
        },
        true
      );
      console.log("result", result);
      assert(result === "3");
    });

    it("auto long text base64", async () => {
      let result = await Cache.auto(
        "test",
        20,
        [],
        async () => {
          let text = "";
          // 循环1000次
          for (let i = 0; i < 1000; i++) {
            text += "a";
          }
          console.log(text);
          return text;
        },
        true
      );
      console.log("result", result);
      assert(result === "3");
    });

    it("auto long text default zip", async () => {
      let text = "";
      // 循环1000次
      for (let i = 0; i < 2000; i++) {
        text += "a";
      }
      console.log(text);
      let result = await Cache.auto("test", 20, [], async () => {
        return text;
      });
      console.log("result", result);
      assert(result === text);
    });

    it("local cache", async () => {
      async function test(a, b) {
        return await Cache.auto("test3", 20, arguments, () => {
          console.log(a, b);
          return { a, b };
        });
      }
      const result = await test(1, 2);
      console.log("cache", Cache.localCache);
      console.log("result", JSON.stringify(result));
      const compare = { a: 1, b: 2 };
      assert(result.a === compare.a);
    });
  });
});
